package com.kfm.hotel.interceptor;

import com.kfm.hotel.util.Constant;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取session中的用户信息
        Object user = request.getSession().getAttribute(Constant.LOGIN_USER);
        if (user == null){
            // 未登录，重定向到登录页面
            response.sendRedirect("/");
            return false;
        } else {
            // 已登录，放行
            return true;
        }
    }
}
